package com.hhwy.framework.persistent.dialect;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.cfg.Environment;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle9iDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

/**
 * <b>类 名 称：</b>HibernateJpaVendorExtAdapter Hibernate的JPA适配器，支持SQLite<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>lihuiming<br/>
 * <b>修 改 人：</b>Administrator<br/>
 * <b>修改时间：</b>2014年8月14日 下午4:26:24<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
public class HibernateJpaVendorExtAdapter extends HibernateJpaVendorAdapter {

	private DatabaseExt database;
	
	protected Class<?> determineDatabaseDialectClass(DatabaseExt database) {
		switch (database) {
		case MYSQL:
			return MySQLDialect.class;
		case ORACLE:
			return Oracle9iDialect.class;
		case SQLITE:
			return SQLiteDialect.class;
			
		default:
			return null;
		}
	}
	@Override
	public Map<String, Object> getJpaPropertyMap() {
		Map<String, Object> jpaProperties = new HashMap<String, Object>();

		if (getDatabasePlatform() != null) {
			jpaProperties.put(Environment.DIALECT, getDatabasePlatform());
		}
		else if (getDatabase() != null) {
			Class databaseDialectClass = determineDatabaseDialectClass(this.database);
			if (databaseDialectClass != null) {
				jpaProperties.put(Environment.DIALECT, databaseDialectClass.getName());
			}
		}

		if (isGenerateDdl()) {
			jpaProperties.put(Environment.HBM2DDL_AUTO, "update");
		}
		if (isShowSql()) {
			jpaProperties.put(Environment.SHOW_SQL, "true");
		}

		return jpaProperties;
	}
	

	public void setDatabase(DatabaseExt database) {
		this.database = database;
	}
}
